package org.jeecg.modules.xl.shengfutong.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
import org.jeecg.modules.xl.shengfutong.config.ShengftConf;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author deco
 * @create 2025-03-11
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PreUnifieAppletdorderRequest {
    private String mchId;          // 商户号(如果有subMchId,一定要存在代理关系)
    private String subMchId;       // 子商户号
    private MchMemberInfo mchMemberInfo;  // 商户终端信息
    private String outTradeNo;     // 商户订单号(商户侧唯一,每次请求都要用最新的单号)
    private String timeExpire;     // 交易结束时间(格式为yyyyMMddHHmmss)，请合理传入时间
    private String notifyUrl;      // 商户通知回调地址
    private String nonceStr;       // 随机字符串
    private String pageUrl;        // 小程序支付完成后同步跳转地址
    private Integer totalFee;      // 订单总金额(单位分)
    private String tradeType;      // 支付方式(详见支付渠道属性列表)
    private String isNeedShare;    // 是否需要分账
    private String extra;          // 支付要素扩展参数
    private String body;           // 商品描述
    private String detail;         // 交易信息
    private Attach attach;         // 附加信息
    private String currency;       // 标准币种
    private String clientIp;       // 用户IP(H5支付时请传用户真实地址 ,支持IPV6)
    private String signType;       // 签名类型
    private String sign;           // 签名 (见签名介绍)

    public static PreUnifieAppletdorderRequest buildPreUnifieAppletdorderRequest(String outTradeNo,int totalFee,String storeCode,String storeName){
        PreUnifieAppletdorderRequest request = new PreUnifieAppletdorderRequest();

        request.setMchId(ShengftConf.MCH_ID);
        request.setOutTradeNo(outTradeNo);

        //request.setMchMemberInfo(new MchMemberInfo(storeCode,storeName));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //过期时间=当前时间+1天
        Date timeExpire = new Date(System.currentTimeMillis()+24*60*60*1000);
        request.setTimeExpire(sdf.format(timeExpire));
        request.setNotifyUrl("https://sltwxapi.bugless.site:8443/jeecg-boot/htmmp/myorder/sftPayNotifyUrl");
        request.setNonceStr(RandomStringUtils.randomAlphanumeric(32));
        request.setTotalFee(totalFee);//单位分
        request.setTradeType("wx_lite");
        request.setBody("商品描述");
        request.setCurrency("CNY");
        request.setClientIp("127.0.0.1");
        request.setSignType("RSA");
        return request;
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class MchMemberInfo{
    private String mchShopCode;    // 门店号-商户系统注册门店的信息
    private String mchShopName;    // 门店名称-商户系统注册门店的信息
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Attach{
    private Long mktAmt;           // 商户营销活动中贴手续费金额
    private String mktPayer;       // 营销付费商户
}